/*River Warren
 *September 7, 2011
 *
 *Updated Oct 16 2011 8:37PM - River Warren, made changes to reflect updates in base class
 *Updated Nov 5, 2011 - River Warren
 *Door.h
 *Creates door object to be used by Room object
 *door has a description and a key code.
 *has two "states": locked and unlocked.
*/

#ifndef DOOR_H_INCLUDED
#define DOOR_H_INCLUDED

#include <ostream>
#include <istream>
#include <string.h>
#include <stdlib.h>
#include "Base.h"


#define DELIMS ",\n"
#define MAX 250

using std::string;
using std::ostream;
using std::istream;

class Door: public Base
{
    public:
        Door():Base("Door")
        {
            locked = true;
            keyID = "";
        }
        Door(string _keyID):Base()
        {
            locked = true;
            keyID = _keyID;
        }

        ~Door(){};

        bool unlock(string itemID)
        {
            if(keyID.compare(itemID) == 0)
            {
                locked = false;
            }
            return !locked;
        }

        bool isLocked(){return locked;}


        string getKeyCode(){return keyID;}
    protected:
        bool locked;
        string keyID;
    private:

    friend ostream& operator<<(ostream& out, Door& d)
    {
        out << d.getDesc() << ',' << d.keyID << ',';
        if(d.locked) out << '1';
        else out << '0';
        return out;
    }

    friend istream& operator>>(istream& in, Door& d)
    {
        char *tk = NULL;// pointers to chars
        char data[MAX] = ""; //to hold line
        in.getline(data,MAX - 1, '\n');
        tk = strtok(data, DELIMS); // get next token
        d.setDesc(tk);
        tk = strtok(NULL, DELIMS); // get next token
        d.keyID = tk;
        tk = strtok(NULL, DELIMS); // get next token
        d.locked = atoi(tk);
        return in;
    }
};

#endif // DOOR_H_INCLUDED

